package com.hua.common.commonview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;

import com.hua.common.commontools.DipUtil;
/**
 * 旋转的指针控件
 * @author huaxiaolin_pc
 *
 */
public class RotatingPointerControl extends LinearLayout {

	private float viewWidth = 0f;
	/**
	 * 绘制中心圆点的画笔 绘制外边圆环的画笔 绘制指针三角形的画笔
	 */
	private Paint pointPaint, ringPaint, trianglePaint;
	/**
	 * 绘制中心圆点的画笔宽度 绘制外边圆环的画笔宽度 绘制指针三角形的画笔宽度
	 */
	private float pointPaintWidth, ringPaintWidth, trianglePaintWidth;
	/**
	 * 控件内边距
	 */
	private float pading=0f;
	/**
	 * 中心圆点半径
	 */
	private float pointRadius=0f;
	/**
	 * 旋转的角度
	 */
	private float AngleCount=270f;
	/**
	 * 动画
	 */
	private ValueAnimator animator=null;


	public RotatingPointerControl(Context context) {
		super(context);
		this.init();
	}

	public RotatingPointerControl(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.init();
	}

	public RotatingPointerControl(Context context, AttributeSet attrs,
								  int defStyle) {
		super(context, attrs, defStyle);
		this.init();
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		this.initData();
		this.initPaint();
	}

	/**
	 * 初始化动画
	 */
	private void initAnim(){
		animator=ValueAnimator.ofFloat(270,630);
		animator.setRepeatCount(Integer.MAX_VALUE);
		animator.setRepeatMode(ValueAnimator.RESTART);
		animator.setInterpolator(new LinearInterpolator());
		animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator valueAnimator) {
				AngleCount=(float) valueAnimator.getAnimatedValue();
				refresh();
			}
		});
		animator.setDuration(60*1000);
	}

	private void refresh(){
		this.invalidate();
	}

	public void start(){
		if(this.animator==null){
			this.initAnim();
		}
		this.animator.start();
	}
	/**
	 * 初始化数据
	 */
	private void initData() {
		this.pointRadius= DipUtil.Dp2px(getContext(), 8);
		pading=DipUtil.Dp2px(getContext(), 10);
		pointPaintWidth=DipUtil.Dp2px(getContext(), 5);
		ringPaintWidth=DipUtil.Dp2px(getContext(), 10);
		trianglePaintWidth=DipUtil.Dp2px(getContext(), 2);
	}

	/**
	 * 初始化画笔
	 */
	private void initPaint() {
		pointPaint = new Paint();
		pointPaint.setAntiAlias(true);
		pointPaint.setStyle(Style.FILL);
		pointPaint.setStrokeWidth(this.pointPaintWidth);
		pointPaint.setAntiAlias(true); // 消除锯齿
		pointPaint.setDither(true);
		pointPaint.setStrokeCap(Cap.ROUND);
		pointPaint.setColor(Color.parseColor("#ffff0000"));


		ringPaint = new Paint();
		ringPaint.setAntiAlias(true);
		ringPaint.setStyle(Style.STROKE);
		ringPaint.setStrokeWidth(this.ringPaintWidth);
		ringPaint.setAntiAlias(true); // 消除锯齿
		ringPaint.setDither(true);
		ringPaint.setStrokeCap(Cap.ROUND);
		ringPaint.setColor(Color.parseColor("#ff00ff00"));


		trianglePaint = new Paint();
		trianglePaint.setAntiAlias(true);
		trianglePaint.setStyle(Style.FILL);
		trianglePaint.setStrokeWidth(this.trianglePaintWidth);
		trianglePaint.setAntiAlias(true); // 消除锯齿
		trianglePaint.setDither(true);
		trianglePaint.setStrokeCap(Cap.ROUND);
		trianglePaint.setColor(Color.parseColor("#ffff0000"));

	}

	private void initViewWidth() {
		this.viewWidth = this.getMeasuredWidth();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		this.drawPoint(canvas);
		this.drawRing(canvas);
		this.drawTriangle(canvas);

	}
	/**
	 * 绘制中心圆点
	 * @param canvas
	 */
	private void drawPoint(Canvas canvas){
		RectF oval1 = new RectF(this.viewWidth/2-this.pointRadius, this.viewWidth/2-this.pointRadius, this.viewWidth/2+this.pointRadius, this.viewWidth/2+this.pointRadius);
		canvas.drawArc(oval1, 0, 360, true, pointPaint);// 小弧形
	}
	/**
	 * 绘制最外边圆环
	 * @param canvas
	 */
	private void drawRing(Canvas canvas){
		RectF oval1 = new RectF(this.pading, this.pading, this.viewWidth-this.pading, this.viewWidth-this.pading);
		canvas.drawArc(oval1, 0, 360, false, ringPaint);// 小弧形
	}

	/**
	 * 绘制三角形
	 * @param canvas
	 */
	private void drawTriangle(Canvas canvas){
		//这里指的是指针的半径
		float tempRadius=this.viewWidth/2-pading-ringPaintWidth;
		float x0=this.viewWidth/2;
		float y0=this.viewWidth/2;

		Path path = new Path();
		path.moveTo((float) (x0+pointRadius*Math.cos(AngleCount/180*Math.PI-Math.PI/2)), (float) (y0+pointRadius*Math.sin(AngleCount/180*Math.PI-Math.PI/2)));// 此点为多边形的起点
		path.lineTo((float)(x0+pointRadius*Math.cos(AngleCount/180*Math.PI+Math.PI/2)), (float)(y0+pointRadius*Math.sin(AngleCount/180*Math.PI+Math.PI/2)));
		path.lineTo((float)(x0+tempRadius*Math.cos(AngleCount/180*Math.PI)),(float)(y0+tempRadius*Math.sin(AngleCount/180*Math.PI)));
		path.close(); // 使这些点构成封闭的多边形
		canvas.drawPath(path, trianglePaint);

	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		this.initViewWidth();
	}
}
